package fr.upstlse.glre.client.administration.vitrine;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Container;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.MarginData;
import com.extjs.gxt.ui.client.widget.tips.ToolTipConfig;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.Widget;

import fr.upstlse.glre.client.administration.vitrine.editeurDePage.RTT.RichTextToolbar;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.vitrine.PageVitrineVOGWT;

//import fr.upstlse.glre.client.vitrine.editeurDePage.RichTextToolbar.RichTextToolbar;

public class Editeur {

	final TabPanel tabPanel = new TabPanel();
	private ContentPanel simple;
	private ContentPanel navPanel;

	public Editeur() {
		navPanel = new ContentPanel();

		tabPanel.setTabScroll(true);


		simple = new ContentPanel();
		simple.setHeaderVisible(false);
		simple.setWidth(800);
		simple.setHeight(600);

//		simple.addText(TestData.DUMMY_TEXT_SHORT);  
		simple.setScrollMode(Scroll.AUTO);



		simple.setLayout(new BorderLayout());  

		ContentPanel west = new ContentPanel();  
		ContentPanel center = new ContentPanel();

		west.setHeading("Pages du site");
		center.setHeading("Edition des pages du site");

		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200);  
		westData.setSplit(true);  
		westData.setCollapsible(true);  
		westData.setMargins(new Margins(5, 5, 5, 5));

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
		centerData.setMargins(new Margins(5, 5, 5, 5));  
		westData.setCollapsible(true);  
		westData.setSplit(true);  

		simple.add(west, westData);  
		simple.add(center, centerData);

		//west.add(navPanel, new FitLayout());
		west.add(navPanel, new MarginData());

//		Container c = new Container();
//		c.add(tabPanel);
		center.add(tabPanel);
		tabPanel.setHeight("100%");

		Button btnNewPage = new Button("Ajouter une nouvelle page", new SelectionListener<ComponentEvent>() {  
//			public void componentSelected(ComponentEvent ce) {  
//				MessageBox.prompt("Nom de la nouvelle page", "Entrer le nom de la nouvelle page ici:", false);
//			}
			public void componentSelected(ComponentEvent ce) {  
				PageVitrineVOGWT lPageVitrineVOGWT = new PageVitrineVOGWT();
				lPageVitrineVOGWT.setTitre("Nouvelle page");
				ajouterUnePage(lPageVitrineVOGWT);
			}
		});
		west.add(btnNewPage);
		btnNewPage.setToolTip(new ToolTipConfig("Information", "Cliquez ici pour ajouter une nouvelle page au site web")); 

		
		AsyncCallback<PageVitrineVOGWT[]> lPagesVitrineCallback = new AsyncCallback<PageVitrineVOGWT[]>() {
			public void onFailure(Throwable pCaught) {
				MessageBox.alert("Erreur", "Erreur getAllPagesVitrine " + pCaught.getMessage(), null);
			}

			public void onSuccess(PageVitrineVOGWT[] pResult) {
				PageVitrineVOGWT[] lPageVitrine = pResult;
				for (PageVitrineVOGWT pageVitrineVOGWT : lPageVitrine) {
					ajouterUnePage(pageVitrineVOGWT);	
//					Info.display(pageVitrineVOGWT.getTitre(), pageVitrineVOGWT.getContenu());
				}
			}
		};
		ControlerUtil.getControlerServiceAsyncInstance().getAllPageVitrine(lPagesVitrineCallback);
	}


	public void ajouterUnePage(final PageVitrineVOGWT pPageVitrineVOGWT){
		Label lab = new Label(pPageVitrineVOGWT.getTitre());
		navPanel.add(lab);
		lab.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {                
				if(tabPanel.findItem(pPageVitrineVOGWT.getTitre(), true)==null){
					final TextField<String> lTitreTextField = new TextField<String>();
					
							lTitreTextField.setValue(pPageVitrineVOGWT
									.getTitre());
					
					final RichTextArea area = new RichTextArea();
					RichTextToolbar tb = new RichTextToolbar(area);

					VerticalPanel g = new VerticalPanel();
					g.add(tb);
					g.add(area);

					area.setWidth("100%");
					area.setHeight("80%");
					area.setHTML(pPageVitrineVOGWT.getContenu());

					tb.setWidth("100%");

					g.setWidth("100%");
					g.setHeight("100%");

					final TabItem itemPanel = new TabItem();
					itemPanel.setId(pPageVitrineVOGWT.getTitre());
					itemPanel.setText(pPageVitrineVOGWT.getTitre());
					itemPanel.setClosable(true);
					itemPanel.setScrollMode(Scroll.AUTO);

					itemPanel.add(lTitreTextField);
					itemPanel.add(tb);
					itemPanel.add(area);

					HorizontalPanel hp = new HorizontalPanel();

					Button b1 = new Button("Enregistrer", new SelectionListener<ComponentEvent>() {  
						public void componentSelected(ComponentEvent ce) {  
							pPageVitrineVOGWT.setTitre(lTitreTextField.getValue());
							pPageVitrineVOGWT.setContenu(area.getHTML());
							if(pPageVitrineVOGWT.getId() == 0) {
							AsyncCallback<Long> lPagesVitrineCreationCallback = new AsyncCallback<Long>() {
								public void onFailure(Throwable pCaught) {
									final MessageBox lFailureMessageBox = MessageBox.alert("Erreur", "Erreur createPageVitrine " + pCaught.getMessage(), null);
									lFailureMessageBox.show();
								}

								public void onSuccess(Long pResult) {
										Info.display("Info","Page crée");
								}
							};
							
								ControlerUtil.getControlerServiceAsyncInstance().createPageVitrine(pPageVitrineVOGWT, lPagesVitrineCreationCallback);
							}
							else {
								AsyncCallback<Object> lPagesVitrineModificationCallback = new AsyncCallback<Object>() {
									public void onFailure(Throwable pCaught) {
										final MessageBox lFailureMessageBox = MessageBox.alert("Erreur", "Erreur updatePage vitrine " + pCaught.getMessage(), null);
										lFailureMessageBox.show();
									}

									public void onSuccess(Object pResult) {
											Info.display("Info","Page modifiée");
									}
								};
								ControlerUtil.getControlerServiceAsyncInstance().updatePageVitrine(pPageVitrineVOGWT, lPagesVitrineModificationCallback);
							}
						}
					});
					b1.setToolTip(new ToolTipConfig("Information", "Cliquez ici pour mettre à jour la page '" + pPageVitrineVOGWT.getTitre() + "' du site web")); 

					Button b2 = new Button("Supprimer la page", new SelectionListener<ComponentEvent>() {  
						public void componentSelected(ComponentEvent ce) {  
							MessageBox.confirm("Confirmation", "Etes vous sur de vouloir supprimer définitivement la page web '" + pPageVitrineVOGWT.getTitre() + "'?", null);  
						}
					}
					
//					itemPanel.close();
					);
					b2.setToolTip(new ToolTipConfig("Information", "Cliquez ici pour supprimer la page '" + pPageVitrineVOGWT.getTitre() + "' du site web")); 

					hp.add(b1);
					hp.add(b2);

					itemPanel.add(hp);

					tabPanel.add(itemPanel);
					tabPanel.setSelection(itemPanel);  
				}
				else{
					tabPanel.setSelection(tabPanel.findItem(pPageVitrineVOGWT.getTitre(), true));
				}
			}
		});
		navPanel.layout();
	}

	private Grid createRichText() {
		RichTextArea area = new RichTextArea();
		RichTextToolbar tb = new RichTextToolbar(area);

		Grid g = new Grid(2,1);
		g.setWidget(0, 0, tb);
		g.setWidget(1, 0, area);

		area.setWidth("100%");

		tb.setWidth("100%");

		g.setWidth("100%");
		g.setHeight("100%");

		DOM.setStyleAttribute(g.getElement(), "margin-right", "4px");
		return g;
	}

	public ContentPanel getSimple() {
		return simple;
	}

	public void setSimple(ContentPanel simple) {
		this.simple = simple;
	}

	public Container getNavPanel() {
		return navPanel;
	}

	public void setNavPanel(ContentPanel navPanel) {
		this.navPanel = navPanel;
	}

	public TabPanel getTabPanel() {
		return tabPanel;
	}

}
